package org.nlp2rdf.navigator.client.functionality;

import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.HasVerticalAlignment;
import com.google.gwt.user.client.ui.HorizontalPanel;
import com.google.gwt.user.client.ui.Label;

public abstract class PaginationWidget extends HorizontalPanel {

	final private Label positionInResultSet = new Label("");
	final private Button paginateUp;
	final private Button paginateDown;
	protected final int increment;
	protected int current = 0;

	public PaginationWidget(int increment) {
		this.increment = increment;

		paginateUp = new Button("+" + this.increment);
		paginateUp.addClickHandler(new ClickHandler() {

			public void onClick(ClickEvent arg0) {
				paginate(true);

			}
		});

		paginateDown = new Button("‒" + this.increment);
		paginateDown.addClickHandler(new ClickHandler() {

			public void onClick(ClickEvent arg0) {
				paginate(false);

			}
		});

		this.setHorizontalAlignment(HorizontalPanel.ALIGN_RIGHT);
		this.setVerticalAlignment(HasVerticalAlignment.ALIGN_MIDDLE);
		this.add(positionInResultSet);
		this.add(paginateDown);
		this.add(paginateUp);
		paginateDown.setEnabled(false);
		paginateUp.setEnabled(false);
		

	}

	public void paginate(boolean up) {
		paginateDown.setEnabled(false);
		paginateUp.setEnabled(false);
		if (up) {
			current += increment;
		} else {
			current -= increment;
		}
		_paginate();
	}

	public void updateIndex(int numberOfRetrievedItems) {
		positionInResultSet.setText("(displaying result " + (current+1) + "-" + (current+numberOfRetrievedItems) + ")");
		if(current == 0){
			getPaginateDown().setEnabled(false);
			getPaginateUp().setEnabled(true);
		}else if(numberOfRetrievedItems==increment){
			getPaginateDown().setEnabled(true);
			getPaginateUp().setEnabled(true);
		}else if(numberOfRetrievedItems<increment){
			getPaginateDown().setEnabled(true);
			getPaginateUp().setEnabled(false);
		}
	}
	
	

	protected abstract void _paginate();

	public Button getPaginateUp() {
		return paginateUp;
	}

	public Button getPaginateDown() {
		return paginateDown;
	}

	public int getIncrement() {
		return increment;
	}

	public int getCurrent() {
		return current;
	}

	public void setCurrent(int current) {
		this.current = current;
	}

}
